package employee.ejb.management;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.security.RolesAllowed;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


import authentication.ejb.usermanagement.Group;
import dtos.ExcursionDTO;
import dtos.HotelDTO;
import dtos.MeanOfTransportationDTO;
import dtos.TransportationTypeDTO;
import dtos.TripDTO;
import model.Excursion;
import model.GetEntities;
import model.Hotel;
import model.MeanOfTransportation;
import model.TransportationType;
import model.Trip;


@Stateless
public class ProductMrgBean implements ProductMgr{

	@PersistenceContext
    private EntityManager em;
	
	private GetEntities e;
	
	@PostConstruct
	public void init(){
		e = new GetEntities();
	}
	
	@Override
	public void saveHotel(HotelDTO hotel){
		Hotel newHotel = new Hotel(hotel);
		em.persist(newHotel);
	}
	
	@Override
	@RolesAllowed({Group._EMPLOYEE,Group._ADMIN})
	public void updateHotel(HotelDTO hotel) {
			em.merge(new Hotel(hotel));
	}
	
	@Override
	public void removeHotel(int hotelid) {
		Hotel h = em.find(Hotel.class,hotelid);
        em.remove(h);
	}
	
	@Override
	public Collection<HotelDTO> getHotelList(){
		@SuppressWarnings("unchecked")
		List<Hotel> qres = em.createQuery("SELECT x FROM Hotel x").getResultList();

		Collection<HotelDTO> res = new ArrayList<HotelDTO>(qres.size());
		for(Hotel h : qres)
		{
			res.add(h.getDTO());
		}
		return res;
	}
	
	
	@Override
	public void saveExcursion(ExcursionDTO excursion){
		Excursion newExcursion = new Excursion(excursion);
		em.persist(newExcursion);
	}
	
	@Override
	@RolesAllowed({Group._EMPLOYEE,Group._ADMIN})
	public void updateExcursion(ExcursionDTO exc) {
			em.merge(new Excursion(exc));
	}
	
	@Override
	public void removeExcursion(int excursionid) {
		Excursion excursion = em.find(Excursion.class,excursionid);
        em.remove(excursion);
	}
	
	@Override
	public Collection<ExcursionDTO> getExcursionList(){
		@SuppressWarnings("unchecked")
		List<Excursion> qres = em.createQuery("SELECT x FROM Excursion x").getResultList();

		Collection<ExcursionDTO> res = new ArrayList<ExcursionDTO>(qres.size());
		for(Excursion e : qres)
		{
			res.add(e.getDTO());
		}
		return res;
	}
	
	@Override
	public void saveTrip(TripDTO trip){
		Trip newTrip = new Trip(trip);
		em.persist(newTrip);
	}
	
	@Override
	public void updateTrip(TripDTO trip) {
			em.merge(new Trip(trip));
	}
	
	@Override
	public void removeTrip(int tripid) {
		Trip trip = em.find(Trip.class,tripid);
		/*String name = trip.getDeparturePlace();
		trip.setDeparturePlace(name + "(not available)");
		em.merge(trip);*/
		em.remove(trip);
	}
	
	@Override
	public Collection<TripDTO> getTripList(){
		@SuppressWarnings("unchecked")
		List<Trip> qres = em.createQuery("SELECT x FROM Trip x").getResultList();

		Collection<TripDTO> res = new ArrayList<TripDTO>(qres.size());
		for(Trip t : qres)
		{
			res.add(t.getDTO());
		}
		return res;
	}
	
	@Override
	public void saveType(TransportationTypeDTO type){
		TransportationType newType = new TransportationType(type);
		em.persist(newType);
	}
	
	@Override
	public void updateType(TransportationTypeDTO type) {
			em.merge(new TransportationType(type));
	}
	
	@Override
	public void removeType(int typeid) {
		TransportationType type = em.find(TransportationType.class,typeid);
        em.remove(type);
	}
	
	@Override
	public Collection<TransportationTypeDTO> getTypeList(){
		@SuppressWarnings("unchecked")
		List<TransportationType> qres = em.createQuery("SELECT x FROM TransportationType x").getResultList();

		Collection<TransportationTypeDTO> res = new ArrayList<TransportationTypeDTO>(qres.size());
		for(TransportationType type : qres)
		{
			res.add(type.getDTO());
		}
		return res;
	}
	
	@Override
	public void saveMean(MeanOfTransportationDTO mean){
		MeanOfTransportation newMean = new MeanOfTransportation(mean);
		TransportationType type = e.getTransportationTypeEntity(mean.getTransportationType());
		newMean.setTransportationType(type);
		Trip trip = e.getTripEntity(mean.getTrip());
		newMean.setTrip(trip);
		em.persist(newMean);
	}
	
	@Override
	public void updateMean(MeanOfTransportationDTO mean) {
		MeanOfTransportation newMean = new MeanOfTransportation(mean);
		TransportationType type = e.getTransportationTypeEntity(mean.getTransportationType());
		newMean.setTransportationType(type);
		Trip trip = e.getTripEntity(mean.getTrip());
		newMean.setTrip(trip);
		em.merge(newMean);
	}
	
	@Override
	public void removeMean(int transportationid) {
		MeanOfTransportation mean = em.find(MeanOfTransportation.class,transportationid);
        em.remove(mean);
	}
	
	@Override
	public Collection<MeanOfTransportationDTO> getMeanList(){
		@SuppressWarnings("unchecked")
		List<MeanOfTransportation> qres = em.createQuery("SELECT x FROM MeanOfTransportation x").getResultList();

		Collection<MeanOfTransportationDTO> res = new ArrayList<MeanOfTransportationDTO>(qres.size());
		for(MeanOfTransportation mean : qres)
		{
			res.add(mean.getDTO());
		}
		return res;
	}
	
	@Override
	public MeanOfTransportationDTO createMeanDTO(int id) {
		MeanOfTransportation mean = em.find(MeanOfTransportation.class, id);
		MeanOfTransportationDTO meandto = mean.getDTO();
		return meandto;
	}
	
}
